package cc.messcat.dao.area;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import cc.messcat.common.dao.BaseDaoImpl;
import cc.messcat.entity.area.Area;

@Repository
public class AreaDaoImpl extends BaseDaoImpl<Area> implements AreaDao, Serializable {
	private static final long serialVersionUID = 7284647613848905105L;

	@Override
    public List<Area> getAreasByParentId(Long parentId) {
        return
//				NumberUtil.isLongZero(parentId) ? Collections.emptyList() :
                getCurrentSession().createQuery(" from Area a where a.fatherId = :parentId")
                        .setParameter("parentId", parentId).list();
    }

    @Override
    public Area getByID(Long areaID) {
        return (Area) getCurrentSession().createCriteria(Area.class).add(Restrictions.eq("id", areaID)).uniqueResult();
    }

    @Override
    public List<Area> retrieveAllarea() {
        return (List<Area>) getCurrentSession()
                .createQuery("from Area ").list();

    }

    @Override
    public void removearea(Long id) {
        //delete(id);
        //deleteById(Area.class, id);
        //getCurrentSession().createQuery("delete from Area  where id="+id).executeUpdate();


    }

    @Override
    public List<Area> getAreaHasVillageInfo(Long fatherId) {
        Query query = getCurrentSession().createQuery("from Area a where a.fatherId=:fatherId and a in (select pv.area from PoorVillage pv) ");
        query.setParameter("fatherId", fatherId);
        return query.list();

    }

    @Override
    public Area getAreaByName(String name) {
        Query query = getCurrentSession().createQuery("from Area a where a.area = :name").setString("name", name);
        return (Area) query.uniqueResult();
    }
    
    @Override
    public Area getAreaByCode(String areaCode) {
        Query query = getCurrentSession().createQuery("from Area a where a.areaCode = :areaCode").setString("areaCode", areaCode);
        return (Area) query.uniqueResult();
    }

	@SuppressWarnings("unchecked")
	@Override
	public List<String> retrieveAllareaCode() {
		  return (List<String>) getCurrentSession().createQuery("select areaCode from Area ").list();
		
	}

	@Override
	public List<Area> areaAuthorization(String areaCode) {
		String sql = " from Area as a where a.areaCode like :areaCode and a.fatherId=3";
		Query query = getCurrentSession().createQuery(sql).setString("areaCode", areaCode);
		
		return query.list();
	}


}
